<template>
  <!-- 上传回执单，告客户知情同意书信息 -->
  <el-dialog title="上传信息" v-model="editVisible" width="60%" draggable>
    <el-form ref="formRef" :model="form" :rules="rules" label-width="150px" v-loading="loading">
      <el-row :gutter="10">
        <el-col :span="10">
          <el-form-item label="纸质回执单：" prop="receipt">
            <UploadImageInfo :imageWidth="178" :imageHeight="178" :limit="600" suggestInfo="" v-model:disabledbtn="disabledbtn" v-model:loading="loading" imageKey="receipt" v-model:form="form" />
          </el-form-item>
        </el-col>
        <el-col :span="10">
          <el-form-item label="告客户知情同意书：" prop="customer_known_pic">
            <UploadImageInfo :imageWidth="178" :imageHeight="178" :limit="600" suggestInfo="" v-model:disabledbtn="disabledbtn" v-model:loading="loading" imageKey="customer_known_pic" v-model:form="form" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" :disabled="disabledbtn" @click="submitForm">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import UploadImageInfo from "@/components/common/UploadImageInfo.vue";
import { useState } from "@/hooks/useState.ts";
import type { IRentreceiptDTO } from "@/types/rentMachineManage/rentMachineOrderInfo";
import { putUpdateImageInfo } from "@/utils/api/admin/order/rentOrder";
const { disabledbtn, loading, toggleState } = useState();

const emit = defineEmits<{
  handleSuccess: [];
}>();
const editVisible = defineModel<boolean>("editVisible", { required: true });
const form = defineModel<IRentreceiptDTO>("form", { required: true });
const formRef = useTemplateRef("formRef");
const rules = reactive({});

watch(
  () => editVisible.value,
  newValue => {
    if (newValue) {
      /* 初始化数据 */
    } else {
      form.value.id = 0;
      form.value.receipt = "";
      form.value.customer_known_pic = "";
      form.value.contract_address = "";
      formRef.value?.resetFields();
      formRef.value?.clearValidate();
    }
  }
);
/* 数据校验 */
function submitForm() {
  if (!formRef.value) return;
  formRef.value.validate(valid => {
    if (!valid) {
      ElMessage.warning("请填写必填字段");
      return;
    }
    submitFormInfo();
  });
}
/* 保存或编辑信息 */
function submitFormInfo() {
  toggleState(true);
  putUpdateImageInfo(form.value)
    .then(res => {
      ElMessage.success(res.msg);
      emit("handleSuccess");
    })
    .catch(() => {})
    .finally(() => {
      toggleState(false);
    });
}
</script>

<style lang="scss" scoped></style>
